﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace SourceMaster.Util
{
    public class FileUtil
    {
        public static string GetFormattedFileSize(string fileFullPath)
        {
            if (!File.Exists(fileFullPath))
                return "--";

            double bytes = new System.IO.FileInfo(fileFullPath).Length;

            string[] suffixes = { "B", "KB", "MB", "GB" };
            int order = 0;
            while(bytes>=1024&&order+1<suffixes.Length)
            {
                order++;
                bytes = bytes / 1024;
            }
            return string.Format("{0:0.##}{1}", bytes, suffixes[order]);
        }

        // 是否含有 byte 0
        public static bool IsBinaryFile(string fileFullPath)
        {
            if (!File.Exists(fileFullPath))
                return false;

            byte[] fileBytes = File.ReadAllBytes(fileFullPath);

            bool hadNullByte = false;

            foreach (byte b in fileBytes)
            {
                if (b == (byte)0)
                {
                    hadNullByte = true;
                    break;
                }
            }

            return hadNullByte;
        }
    }
}
